package com.example.activitytest;

import android.Manifest;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.activitytest.bean.Student;
import com.example.activitytest.bean.Teacher;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private static final int CODE = 1;
    private EditText medit;
    private Button mbutton;
    private Button mbutton1;
    private Button mbutton2;
    private Button mbutton3;
    private  Button mbuttonCall;
    private Button mbuttonDial;
    private Button mbuttonHistory;
    private Button mbuttonSendTo;
    private Button mbuttonview;
    private Button mButtonSearch;
    private Button mButtonSend;
    private Button mButtonCapture;
    private ImageView mPhoto;
    private ActivityResultLauncher mActivityResultLauncher;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Log.d("TAG", "onCreate: ");

        medit = findViewById(R.id.edit);
        mbutton = findViewById(R.id.button);
        mbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent("com.jn.test123");
//                intent.putExtra("name",medit.getText().toString());
//                intent.putExtra("boolean",true);
//                byte b = 1;
//                intent.putExtra("byte",b);
//                intent.putExtra("char",'A');
//                intent.putExtra("short",(short)2);
//                intent.putExtra("int",1);
//                intent.putExtra("long",4L);
//                intent.putExtra("float",1.1F);
//                intent.putExtra("double",2.2);
//                Bundle bundle = new Bundle();
//                bundle.putBoolean("boolean",true);
//                bundle.putInt("int",1);
//                intent.putExtra("bundle",bundle);
//
//                int[] array={1,2,3,4};
//                intent.putExtra("array",array);
//
//                ArrayList<String> list = new ArrayList<>();
//                list.add("a");
//                list.add("b");
//                list.add("c");
//                intent.putStringArrayListExtra("list",list);
//
//                    Student student=new Student();
//                    student.setAge(21);
//                    student.setName("jim");
//                    student.setNumber(20222020);
//
//                    intent.putExtra("student",student);
//
//
//                Teacher teacher=new Teacher();
//                teacher.setAge(66);
//                teacher.setName("tom");
//                teacher.setNumber(19880002);
//                intent.putExtra("teacher",teacher);
//                startActivity(intent);
                Intent intent=new Intent(MainActivity.this);
            }
        });
        mbutton1 = findViewById(R.id.button1);
        mbutton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName(
                        "com.jn.ActivityTest",
                        "com.jn.ActivityTest.SecondActivity");
                intent.setComponent(componentName);
                intent.putExtra("name",medit.getText().toString());
                startActivity(intent);
            }
        });
        mbutton2 = findViewById(R.id.button2);
        mbutton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClassName("com.jn.ActivityTest",
                        "com.jn.ActivityTest.SecondActivity");
            }
        });
        mbutton3 = findViewById(R.id.button3);
        mbutton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClassName("com.jn.layouttest_1",
                        "com.jn.layouttest_1.LayoutActivity");
                intent.putExtra("name",medit.getText().toString());
                startActivity(intent);
            }
        });
        mbuttonCall = findViewById(R.id.button_call);
        mbuttonCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this,
                            new String[]{Manifest.permission.CALL_PHONE},CODE);
                    
                }else {
                    callPhone();
                }
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:10000"));
                startActivity(intent);
            }
        });

        mbuttonDial = findViewById(R.id.button_dail);
        mbuttonDial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);

            }
        });

        mbuttonHistory = findViewById(R.id.button_history);
        mbuttonHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
                startActivity(intent);
            }
        });

        mbuttonSendTo = findViewById(R.id.button_sendTo);
        mbuttonSendTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                发送邮件
//                Uri uri = Uri.parse("mailto:1234656789@qq.com");
//                Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
//                intent.putExtra(Intent.EXTRA_SUBJECT,"你好");
//                intent.putExtra(Intent.EXTRA_TEXT,"内容");
//                startActivity(intent);
                Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:13027221477"));
                intent.putExtra("sms_body","你好，这是一封信");
                startActivity(intent);

            }
        });

        mbuttonview = findViewById(R.id.button_actionview);
        mbuttonview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Uri uri = Uri.parse("http://www.baidu.com");
//                Uri uri = Uri.parse("tel:10086");
                Uri uri = Uri.parse("");
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
            }
        });
        mButtonSearch = findViewById(R.id.button_search);
        mButtonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                intent.putExtra(SearchManager.QUERY, "JLASTU");
                startActivity(intent);
            }
        });
        mButtonSend = findViewById(R.id.button_send);
        mButtonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "Hello, everyone!");
                startActivity(Intent.createChooser(intent, "choose one"));
            }
        });
        mButtonCapture = findViewById(R.id.button_capture);
        mPhoto = findViewById(R.id.photo);
        mActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode() == RESULT_OK) {
                            Bitmap bitmap = result.getData().getParcelableExtra("data");
                            mPhoto.setImageBitmap(bitmap);
                        }
                    }
                });
        mButtonCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                mActivityResultLauncher.launch(intent);
            }
        });

    }
    private void callPhone(){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:10000"));
        startActivity(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults, int deviceId) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults, deviceId);
        if (requestCode == CODE){
            if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                callPhone();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("TAG", "onResume: ");
        medit.setText("");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("TAG", "onCreate: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("TAG", "onCreate: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("TAG", "onCreate: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("TAG", "onCreate: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG", "onCreate: ");

    }

}